const express = require('express');
// 创建一个路由
const router = express.Router();
const mysql = require('../../conn');
const getDate = require('../../utils');



// 查询食谱
router.get('/get', (req, res) => {
    // console.log(req.body);
    // res.send('接口测试成功');
    // 定义一个对象来接收我们返回给前端的数据
    let obj = {};
    // 数据库的查询语句
    let sql = `select * from cookbook`;

    mysql.query(sql, (err, results) => {
        if (err) return console.log(err);
        obj.code = 200;
        obj.desc = '成功';
        obj.data = results;
        res.send(obj);
    });
});

// id查询食谱
router.get('/info', (req, res) => {
    let { cook_book_id } = req.query;
    // console.log(req.body);
    // res.send('接口测试成功');
    // 定义一个对象来接收我们返回给前端的数据
    let obj = {};
    // 数据库的查询语句
    let sql = `select * from cookbook where cook_book_id = ?`;

    mysql.query(sql, [cook_book_id], (err, results) => {
        if (err) return console.log(err);
        obj.code = 200;
        obj.desc = '成功';
        obj.data = results;
        res.send(obj);
    });
});

// 增加食谱
router.post('/add', (req, res) => {
    let { cook_book_title, cook_book_image, describe, material, meals, click_count, collect_count, user_id, user_name } = req.body;
    let create_time = getDate();
    let last_update_time = getDate();
    // console.log(req.body);
    // res.send('接口测试成功');
    // 定义一个对象来接收我们返回给前端的数据
    mysql.query(`select user_name from user where user_id = ?`, [user_id], (err, results) => {
        user_name = results[0].user_name;
        let obj = {};
        // 数据库的增加语句
        let sql = `insert into cookbook (cook_book_title,cook_book_image,\`describe\`,material,meals,click_count,collect_count,create_time ,last_update_time,user_id,user_name) value (?,?,?,?,?,?,?,?,?,?,?)`;

        mysql.query(sql, [cook_book_title, cook_book_image, describe, material, meals, click_count, collect_count, create_time, last_update_time, user_id, user_name], (err, results) => {
            if (err) return console.log(err);
            obj.code = 0;
            obj.desc = '成功';
            obj.data = results;
            res.send(obj);

        });
    })


});

// 删除食谱
router.put('/delete', (req, res) => {
    let { cook_book_id } = req.body;
    // console.log(req.body);
    // res.send('接口测试成功');
    // 定义一个对象来接收我们返回给前端的数据
    let obj = {};
    // 数据库的增加语句
    let sql = `delete from cookbook where cook_book_id in (?)`;

    mysql.query(sql, [cook_book_id], (err, results) => {
        if (err) return console.log(err);
        obj.code = 0;
        obj.desc = '成功';
        // obj.data = results;
        res.send(obj);
    });
});

// 修改食谱
router.put('/update', (req, res) => {
    let { cook_book_title, cook_book_image, describe, material, meals, click_count, collect_count, user_id, user_name, cook_book_id } = req.body;
    let last_update_time = getDate();
    // console.log(req.body);
    // res.send('接口测试成功');
    // 定义一个对象来接收我们返回给前端的数据
    let obj = {};
    // 数据库的增加语句
    mysql.query(`select user_name from user where user_id = ?`, [user_id], (err, results) => {
        user_name = results[0].user_name;
        let sql = `update cookbook set cook_book_title=?, cook_book_image=?, \`describe\`=?, material=?, meals=?, click_count=?, collect_count=?, user_id=?, user_name=? ,last_update_time=?  where cook_book_id in (?);`;

        mysql.query(sql, [cook_book_title, cook_book_image, describe, material, meals, click_count, collect_count, user_id, user_name, last_update_time, cook_book_id], (err, results) => {
            if (err) return console.log(err);
            obj.code = 0;
            obj.desc = '成功';
            obj.data = results;
            res.send(obj);
        });
    })
    
});


// 加入购买
router.post('/buy', (req, res) => {
    let { user_id, cook_book_id } = req.body;
    // console.log(req.body);
    // res.send('接口测试成功');
    // 定义一个对象来接收我们返回给前端的数据
    let obj = {};
    // 数据库的增加语句
    let sql = `insert into user_cook (user_id,cook_book_id) value (?,?)`;
    mysql.query(sql, [user_id, cook_book_id], (err, results) => {
        if (err) return console.log(err);
        obj.code = 200;
        obj.desc = "成功";
        obj.data = {
            "id": results.insertId
        }
        res.send(obj);

    });
})

// 移除购买
router.post('/cancelbuy', (req, res) => {
    let { user_id, cook_book_id } = req.body;
    // console.log(req.body);
    // res.send('接口测试成功');
    // 定义一个对象来接收我们返回给前端的数据
    let obj = {};
    // 数据库的增加语句
    let sql = `delete from user_cook where user_id in (?) and cook_book_id in (?)`;
    mysql.query(sql, [user_id, cook_book_id], (err, results) => {
        if (err) return console.log(err);
        obj.code = 200;
        obj.desc = "成功";
        res.send(obj);

    });
})

// 查询购买
router.get('/getbuy', (req, res) => {
    let obj = {};
    let cookArr = [];
    let { user_id } = req.query;
    mysql.query(`select cook_book_id from user_cook where user_id = ?`, [user_id], (err, results) => {
        if (err) return console.log(err);

        for (let i = 0; i < results.length; i++) {
            mysql.query(`select * from cookbook where cook_book_id = ?`, [results[i].cook_book_id], (err, result) => {
                cookArr.push(result[0]);
            })
        }
        setTimeout(() => {
            obj.code = 200;
            obj.desc = "成功";
            obj.data = cookArr;
            res.send(obj);
        }, 1000);


    });
})
module.exports = router;